<?php
/***
门店管理员登录
***/
namespace app\dboard\controller;
use think\captcha;
use think\facade\Cookie;
use think\facade\Session;
use think\facade\Url;
use think\Db;
use app\api\logic\Base as apiBase;
class Mlogin extends Base
{
    public function initialize() {
        parent::initialize();
    }
    public function index(){
        
    }
    /***登录***/
    public function sign(){
        return view('login/msign_in');
    }
    /**提交登录***/
    public function signIn(){
        if(!$this->request->isAjax()){
            $this->res_data['Msg']='请用正确方式登录！';
            return $this->print_result($this->res_data);
        }
        $user_name=trim($this->request->post('user_name',''));
        $password=trim($this->request->post('password',''));
        if(empty($user_name)){
            $this->res_data['Msg']='登录名不能为空！';
            return $this->print_result($this->res_data);
        }
        if(empty($password)){
            $this->res_data['Msg']='登录密码不能为空！';
            return $this->print_result($this->res_data);
        }
        $password=get_admin_pwd($password);
        $where=array();
        $where[]=['account','=',$user_name];
        $where[]=['password','=',$password];
        $info=Db::name('store')->where($where)->find();
        if($info){
            if($info['status'] ==0){
                $this->res_data['Msg']='帐号等待审核,请联系管理员!';
                return $this->print_result($this->res_data);
            }
            if($info['status'] == 2){
                $this->res_data['Msg']='帐号已锁定!';
                return $this->print_result($this->res_data);
            }
            $data=array();
            $data['id']=$info['id'];
            $data['updatetime']=time();
            $data['last_login_ip']=$this->request->ip();
            $data['login_times']=$info['login_times']+1;
            $token_data['created_at'] = time();
            $token_data['expires_in'] = 7200;
            $token_data['ip'] = $data['last_login_ip'];
            $token_data['uid'] = $info['id'];
            $data['token'] = (new apiBase())->createUserToken($token_data);
            $sesson_admin=array();
            $sesson_admin['AdminID']=$info['id'];
            $sesson_admin['sid']=$info['id'];
            $sesson_admin['AdminPhone']=$info['account'];
            $sesson_admin['AdminName']=$info['name'];
            $sesson_admin['AdminToken']=$data['token'];
            Session::set("AdminAccount",$sesson_admin);
            Db::name('store')->update($data,['id'=>$info['id']]);
            $this->res_data['Code']=0;
            $strCookie=$sesson_admin['AdminID']."\t".$sesson_admin['AdminPhone']."\t".$sesson_admin['AdminName']."\t".$sesson_admin['AdminToken']."\t".$sesson_admin['sid'];
            $encode=uc_authcode($strCookie,'ENCODE','ZSyadmin');
            Cookie::set('ZSyadminauths',$encode,array('expire'=>3600*7));
            $this->res_data['Data']='/dboard/shop/shop_info';
            $this->res_data['sid']=$info['id'];
            return $this->print_result($this->res_data);
        }
        $this->res_data['Msg']='登录名或密码不正确！';
        return $this->print_result($this->res_data);
    }
    /**退出**/
    public function signOut(){
        Session::clear();
        Cookie::delete('ZSyadminauths');
        Cookie::clear();
        $isTrue = Session::has('AdminAccount.AdminID');
        if(!$isTrue){
            return $this->redirect('dboard/mlogin/sign');
        }
        return $this->error('退出失败');
    }
}
